/**
 * @Company:中享思途   
 * @Title:AddressServiceImpl.java 
 * @Author:IL Clown   
 * @Date:2020年8月24日 下午5:29:43     
 */
package com.situ.shopsystem.service.Impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import com.situ.shopsystem.dao.AddressDao;
import com.situ.shopsystem.pojo.Address;
import com.situ.shopsystem.pojo.User;
import com.situ.shopsystem.service.AddressService;

/** 
 * @ClassName:AddressServiceImpl 
 * @Description:(这里用一句话描述这个类的作用)  
 */
@Service
public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressDao addressDao;

	/** 
	 * @Title: save 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @param address
	 * @return  
	 */
	@Override
	public int save(Address address, HttpSession session) {
		User user = (User) session.getAttribute("user");
		address.setCreater(user.getRowId());
		address.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		address.setActiveFlag(1);
		address.setIsDefault(0);
		address.setUserId(user.getRowId());
		return addressDao.save(address);
	}

	/** 
	 * @Title: delete 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @param rowId
	 * @return  
	 */
	@Override
	public int delete(Long rowId) {
		return addressDao.delete(rowId);
	}

	/** 
	 * @Title: update 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @param address
	 * @return  
	 */
	@Override
	public int update(Address address) {
		return 0;
	}

	/** 
	 * @Title: findAll 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @return  
	 */
	@Override
	public List<Address> findAll(Long userId) {
		return addressDao.findAllByUserId(userId);
	}

	/** 
	 * @Title: findById 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @param rowId
	 * @return  
	 */
	@Override
	public Address findById(Long rowId) {
		return addressDao.findById(rowId);
	}

	/** 
	 * @Title: updateDefault 
	 * @Description:(这里用一句话描述这个方法的作用)
	 * @param rowId
	 * @param session
	 * @return  
	 */
	@Override
	public int updateDefault(Long rowId, HttpSession session) {
		User user = (User) session.getAttribute("user");
		addressDao.updateNotDefaultByUserId(user.getRowId());

		Address address = addressDao.findById(rowId);
		address.setIsDefault(1);
		addressDao.update(address);

		return 0;
	}

}
